export default class LoopTimer {
  constructor(callback, interval) {
    this.callback = callback;
    this.interval = interval;
    this.timerId = null;
    this.running = false;
  }

  start() {
    if (this.running) return;
    this.running = true;

    // 立即执行一次
    this.callback();

    // 开始循环定时器
    this._loop();
  }

  _loop() {
    if (!this.running) return;
    this.timerId = setTimeout(() => {
      this.callback();
      this._loop();
    }, this.interval);
  }

  pause() {
    this.running = false;
    clearTimeout(this.timerId);
  }

  resume() {
    if (this.running) return;
    this.running = true;
    this._loop();
  }

  reset(newInterval) {
    this.pause();
    this.interval = newInterval !== undefined ? newInterval : this.interval;
  }
}
